<?php 
if (!defined("BASEPATH")) exit("No direct script access allowed");

	class MY_Controller extends CI_Controller {

		function __construct() {
			parent::__construct();
			restore_error_handler();
			error_reporting();
			date_default_timezone_set("Europe/Budapest");
			define("MEDIA", $this->config->item("MEDIA"));
			define("UPLOADS", $this->config->item("UPLOADS"));
			define("PDFS", $this->config->item("PDFS"));
			$lastChar = substr($_SERVER["REQUEST_URI"], -1);
			if(!stristr($_SERVER["REQUEST_URI"], "sitemap.xml") !== FALSE and !stristr($_SERVER["REQUEST_URI"], "file_upload") !== FALSE and !stristr($_SERVER["REQUEST_URI"], "openid") !== FALSE and !in_array($this->uri->segment(1), array("working")) and !$this->input->is_ajax_request() and $lastChar <> "/" and $this->uri->segment(1)) {
				redirect(current_url(), "location", 301);
			}

			/*---===--- BEGIN karakterkodolas beallitasa ---===---*/
			iconv_set_encoding("internal-encoding", "UTF-8");
			iconv_set_encoding("input-encoding", "UTF-8");
			iconv_set_encoding("output-encoding", "UTF-8");
			mb_internal_encoding("UTF-8");
			/*---===--- E N D karakterkodolas beallitasa ---===---*/

			/*---===--- BEGIN tranzakcio kezeles inditasa ---===---*/
			//$this->db->query("begin");
			$this->db->query("SET autocommit = 0");
			$this->db->trans_start();
			/*---===--- E N D tranzakcio kezeles inditasa ---===---*/

			/*---===--- BEGIN cookie beleptetes ---===---*/
			if(!$this->session->userdata("usrId")) {
				$this->load->helper("cookie");
				$usrEmail = get_cookie("usrEmail", TRUE);
				$usrPassword= get_cookie("usrPassword", TRUE);
				if($usrEmail && $usrPassword) {
					$this->load->model("user");
					$getUser = $this->user->getUser("usr_email = '".$usrEmail."' and usr_password = '".$usrPassword."' and usr_verify is null");
					if($getUser) {
						$getUser = current($getUser);
						$this->session->set_userdata("usrId", $getUser["usrId"]);
						$this->session->set_userdata("usrEmail", $getUser["usrEmail"]);
						$this->session->set_userdata("usrNewsletter", $getUser["usrNewsletter"]);
						$this->session->set_userdata("grpName", $getUser["grpName"]);
						$this->user->update($getUser["usrId"], array("usr_logged" => $getUser["usrLogged"]+1));
					}
				}
			}
			/*---===--- E N D cookie beleptetes ---===---*/

			/*---===--- BEGIN cache torles ---===--- */
			if($this->uri->segment(1) != "daemonc" and $this->daemonc_model->getDeamonc(date("Y-m-d")) === 0) {
				$this->db->cache_delete_all();
				$this->load->library("daemon");
				$this->daemon->execute_background();
			} elseif(rand(1,1000) == 1 and !$this->input->is_ajax_request()) { 
				$this->db->cache_delete_all();
			}
			/*---===---  E N D cache torles ---===--- */

			/*---===---  BEGIN allando page valtozok feltoltese ---===--- */
			if(!in_array($this->uri->segment(1), array("admin", "admin_lte"))) {
				$load_vars = new stdClass();
				$this->load->model("country");
				$load_vars->load_vars_getAllCountry = $this->country->getCountry($where = "cou_presenter = 'I'");

				$this->load->model("article_model");
				$load_vars->load_vars_getArticles = $this->article_model->getArticle($where = "art_highlight = 'N' and art_position in ('H', 'F', 'R') and art_name is not null", $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "artOrder", $dir = "DESC");

				$this->load->model("country");
				$load_vars->load_vars_getCountry = $this->country->getCountry($where = "cou_presenter = 'I'",  $start = 0, $limit = 6, $fields = NULL, $query = NULL, $order = "rand()", $dir = NULL);

				$this->load->model("city");
				$load_vars->load_vars_getCity = $this->city->getCity($where = "city_presenter = 'I'",  $start = 0, $limit = 6, $fields = NULL, $query = NULL, $order = "rand()", $dir = NULL);

				$this->load->model("major");
				$load_vars->load_vars_getMajor = $this->major->getMajor($where = "maj_presenter = 'I'",  $start = 0, $limit = 6, $fields = NULL, $query = NULL, $order = "rand()", $dir = NULL);
				
				$this->load->model("ent2image");
				$load_vars->load_vars_getEnt2imageMainSlider = $this->ent2image->getEnt2image($where = "e2i_main_slider_id = -1");

				$this->load->vars($load_vars);
			}
			/*---===---  E N D allando page valtozok feltoltese ---===--- */
			//$this->output->enable_profiler(TRUE);
		}

		public function __destruct() {
		}

	}